import time

from selenium import webdriver#webdriver对象
from selenium.webdriver.edge.options import Options#options选项因webdriver类型而异，这里使用edge
from selenium.webdriver.common.by import By#定位元素By类
from selenium.webdriver.common.keys import Keys#模拟按键操作Keys类
from selenium.webdriver.support.ui import WebDriverWait#显性等待
from selenium.webdriver import ActionChains#用来模拟人为操作鼠标的一系列动作
from selenium.webdriver.support import expected_conditions as EC#条件等待，配合显性等待使用
from selenium.common.exceptions import TimeoutException#超时错误
from selenium.common.exceptions import NoSuchElementException#元素定位错误
from selenium.webdriver.edge.options import Options#Edge浏览器

options=Options()#先声明一个options变量
options.add_argument('--disable-blink-features=AutomationControlled')#隐藏自动化控制标头
options.add_experimental_option('excludeSwitches',['enable-automation'])#隐藏自动化标头
options.add_argument('--ignore-ssl-errosr')#忽略ssl错误
options.add_argument('--ignore-certificate-errors')#忽略证书错误
prefs = {
     'download.default_directory': '文件夹路径',  # 设置文件默认下载路径
     "profile.default_content_setting_values.automatic_downloads": True  # 允许多文件下载
 }
options.add_experimental_option("prefs", prefs)#将prefs字典传入options
options.add_experimental_option("prefs",{"credentials_enable_service":False,"profile.password_manager_enabled":False}) # 禁止弹出保存密码弹窗

browser=webdriver.ChromiumEdge(options)#将options传入到webdriver中

action=ActionChains(browser)

# 全局等待，每进入一个新页面，页面的元素渲染需要时间，我们需要先等待渲染完毕后再进行获取，如下的方式设置了全局等待时间
browser.implicitly_wait(5)

# 访问登陆界面
browser.get("http://localhost:3000/login")

# 模拟输入
username_input = browser.find_element(By.XPATH, '//*[@id="username"]')
password_input = browser.find_element(By.XPATH, '//*[@id="password"]')
username_input.send_keys('root')
time.sleep(1)
password_input.send_keys('123456')
time.sleep(1)
# click
browser.find_element(By.XPATH, '//*[@id="root"]/div/div/div/div[1]/button').click()
time.sleep(1)
# WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="root"]/div/div/div/aside/div/ul/li[2]/div')))

# move to info
info_span = browser.find_element(By.XPATH, '//*[@id="root"]/div/div/header/span[2]')
action.move_to_element(info_span).perform()
time.sleep(1)

# click my account
account_span = browser.find_element(By.XPATH, '/html/body/div[3]/div/ul/li[1]/span[2]')
account_span.click()
# 睡眠2s便于观察
time.sleep(2)

# click "交易策略"下拉菜单 XPATH=//*[@id="root"]/div/div/div/aside/div/ul/li[2]/div
strategy_button = browser.find_element(By.XPATH, '//*[@id="root"]/div/div/div/aside/div/ul/li[2]/div')
strategy_button.click()
# 睡眠2s便于观察
time.sleep(2)

# 获取 "交易策略"菜单的aria-controls属性=rc-menu-uuid-35471-2-/strategy-popup
aria_controls_value = strategy_button.get_attribute('aria-controls')
# 注意到该属性值可以一定程度上决定子菜单的XPath（准确讲是rc-menu-uuid-54118-2-/strategy-popup）

# click "策略管理"子菜单 XPATH=//*[@id="rc-menu-uuid-35471-2-/strategy-popup"]/li[1]
# browser.find_element(By.XPATH, '//*[@id="rc-menu-uuid-35471-2-/strategy-popup"]/li[1]/span[2]').click()
# click "策略管理"子菜单
browser.find_element(By.XPATH, f'//*[@id="{aria_controls_value}"]/li[1]/span[2]').click()

# print("hello")
input("按任意键继续")